const _ = require("lodash");
const { last } = require("lodash");
const fp = require("lodash/fp");
const { Maybe, Container } = require("./support");

// setTimeout(function () {
//   var a = "hello";
//   setTimeout(function () {
//     var b = "lagou";
//     setTimeout(function () {
//       var c = "I ♥ U";
//       console.log(a + b + c);
//     }, 10);
//   }, 10);
// }, 10);

// const promise = new Promise(resolve => {
//   resolve("hello");
// });

// promise
//   .then(a => {
//     return a + "lagou";
//   })
//   .then(b => {
//     console.log(b + "I ♥ U");
//   });

const cars = [
  { name: "Ferrari FF", horsepower: 660, dollar_value: 700000, in_stock: true },

  {
    name: "Spyker C12 Zagato",
    horsepower: 650,
    dollar_value: 648000,
    in_stock: false,
  },

  {
    name: "Jaguar XKR-S",
    horsepower: 550,
    dollar_value: 132000,
    in_stock: false,
  },

  { name: "Audi R8", horsepower: 525, dollar_value: 114200, in_stock: false },

  {
    name: "Aston Martin One-77",
    horsepower: 750,
    dollar_value: 1850000,
    in_stock: true,
  },

  {
    name: "Pagani Huayra1",
    horsepower: 700,
    dollar_value: 1300000,
    in_stock: false,
  },
];

// let isLastInStock = function (cars) {
//   let last_car = fp.last(cars);
//   return fp.prop("in_stock", last_car);
// };
// let isLastInStock = fp.flowRight(fp.prop("in_stock"), fp.last);

// let isLastInStock = fp.flowRight(fp.prop("name"), fp.first);

// console.log(isLastInStock(cars));

// let _average = function (xs) {
//   return fp.reduce(fp.add, 0, xs) / xs.length;
// };

// let _dollarValues = function (cars) {
//   return fp.map(function (car) {
//     return car.dollar_value;
//   }, cars);
// };

// let averageDollarValue = fp.flowRight(_average, _dollarValues);

// console.log(averageDollarValue(cars)); // 790700

let _underscore = fp.replace(/\W+/g, "_");

let sanitizeNames = fp.flowRight(
  fp.map(item => {
    return (item.name = _underscore(item.name.toLowerCase()));
  })
);

console.log(sanitizeNames(cars));

// let maybe = Maybe.of([5, 6, 1]);

// let ex1 = value => maybe.map(arr => fp.map(fp.add(value), arr));
// console.log(ex1(1)); // Maybe { _value: [ 6, 7, 2 ] }

// let xs = Container.of(["do", "ray", "me", "fa", "so", "la", "ti"]);

// let ex2 = xs.map(arr => fp.first(arr));

// console.log(ex2); // Container { _value: 'do' }

// let safeProp = fp.curry(function (x, o) {
//   return Maybe.of(o[x]);
// });

// let user = { id: 2, name: "Albert" };

// let ex3 = safeProp("name", user).map(value => fp.first(value));

// console.log(ex3);

// let maybe = Maybe.of(1.2);

// let ex4 = maybe.map(parseInt);

// console.log(ex4);
